package cn.csxw.openchina.ui.fragment.moveFragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Toast;

import javax.inject.Inject;

import cn.csxw.openchina.base.BaseRecAdapter;
import cn.csxw.openchina.base.BaseRefreshFragment;
import cn.csxw.openchina.dagger2.component.DaggerNesMoveComponent;
import cn.csxw.openchina.dagger2.module.NewMoveModule;
import cn.csxw.openchina.dagger2.presenter.NewMovePresenter;
import cn.csxw.openchina.ui.adapter.MoveAdapter;
import cn.csxw.openchina.utils.Global;

/**
 * 动弹Fragment下的 最新动弹Fragment
 */

public class NewMoveFragment extends BaseRefreshFragment {
    @Inject
    NewMovePresenter mNewMovePresenter;
    public MoveAdapter mMoveAdapter;
    private int pageIndex = 0;

    @Override
    protected BaseRecAdapter getAdapter() {
        MoveAdapter moveAdapter =  new MoveAdapter();
        this.mMoveAdapter = moveAdapter;
        return moveAdapter;
    }

    @Override
    public void doRefresh() {
        mSrfLayout.setRefreshing(true);
        mNewMovePresenter.setIsRefresh();
        loadDate(0);   //  设加载数据及更新界面状态
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (mNewMovePresenter == null) {
            DaggerNesMoveComponent.builder()
                    .newMoveModule(new NewMoveModule(this))
                    .build()
                    .in(this);
        }
        doRefresh();
        setLoadMore();   //  设置加载更多
    }

    /**
     * 设置加载更多
     */
    private void setLoadMore() {
        setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onUpLoadMore() {
                if (Global.isNetworkReachable(Global.mContext)){
                    pageIndex += 1;
                    loadDate(pageIndex);   //加载数据及更新界面状态
                }else {
                    Toast.makeText(getActivity(), "网络连接异常,无法加载更多",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 加载数据及更新界面状态
     * @param pageIndex
     */
    private void loadDate(int pageIndex) {
        //TODO: CallNetWork中记得根据常数类型个数传Object对象 如:
        mNewMovePresenter.callNetWork(0,pageIndex,20);
        Global.onNetworkStateListener(mNewMovePresenter,this);
    }
}
